package com.bauer.batch.config;

import com.bauer.batch.bean.Order;
import com.bauer.batch.reader.TxtReader;
import com.bauer.batch.writer.MysqlWriter;
import java.util.List;
import java.util.function.Function;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @description:
 * @author: weirui
 * @create: 2020-07-28 16:53
 **/
@Configuration
@EnableBatchProcessing
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;


    @Bean
    public TaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(3);
        return executor;
    }

    @Bean
    protected Step insurance() {
        return steps.get("insurance").<String[], Order>chunk(3).reader(reader()).processor(new ItemProcessor<String[],Order>(){
            @Override
            public Order process(String[] item) throws Exception {
                Order order = new Order();
                order.setOrderName(item[0]);
                order.setOrderNo(item[1]);
                return order;
            }
        }).writer(writer()).taskExecutor(taskExecutor())
            .build();
    }


    @Bean
    protected Step step() {
        return steps.get("step").<String[], Order>chunk(3).reader(reader()).processor(new ItemProcessor<String[],Order>(){
            @Override
            public Order process(String[] item) throws Exception {
                Order order = new Order();
                order.setOrderName(item[0]);
                order.setOrderNo(item[1]);
                return order;
            }
        }).writer(writer()).taskExecutor(taskExecutor())
            .build();
    }

    @Bean
    protected ItemReader<String[]> reader() {
        TxtReader reader = new TxtReader();
        return reader;
    }

    @Bean
    protected ItemWriter<Object> writer() {
        return new MysqlWriter();
    }

    @Bean
    public Job footballJob() {
        return this.jobs.get("footballJob")
            .start(step())
            .build();
    }
}
